package com.ning.weixin.utils;

import java.security.Key;
import java.security.SecureRandom;
import java.security.Security;
import java.util.Arrays;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.spec.SecretKeySpec;

import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.pqc.math.linearalgebra.ByteUtils;

/**
 * @author MENG_LI_DONG
 * @version 2018年12月22日 上午12:10:24
 */
public class Sm4Util {
	static {
		Security.addProvider(new BouncyCastleProvider());
	}
	private static final String ENCODING = "UTF-8";
	public static final String ALGORITHM_NAME = "SM4";
	public static final String ALGORITHM_NAME_ECB_PADDING = "SM4/ECB/PKCS7Padding";
	public static final int DEFAULT_KEY_SIZE = 128;
	private static Cipher generateEcbCipher(String algorithmName, int mode, byte [] key)throws Exception {
		Cipher cipher = Cipher.getInstance(algorithmName, BouncyCastleProvider.PROVIDER_NAME);
		Key sm4Key = new SecretKeySpec(key, ALGORITHM_NAME);
		cipher.init(mode, sm4Key);
		return cipher;
	}
	public static byte [] generateKey() throws Exception {
		return generateKey(DEFAULT_KEY_SIZE);
	}
	public static byte [] generateKey(int keySize) throws Exception{
		KeyGenerator kg = KeyGenerator.getInstance(ALGORITHM_NAME, BouncyCastleProvider.PROVIDER_NAME);
		kg.init(keySize, new SecureRandom());
		return kg.generateKey().getEncoded();
	}
	public static String encryptEcb(String hexKey, String paramStr)throws Exception{
		String ciperText = "";
		byte[]keyData = ByteUtils.fromHexString(hexKey);
		byte[]srcData = paramStr.getBytes(ENCODING);
		byte [] cipherArray = encrypt_Ecb_Padding(keyData, srcData);
		ciperText = ByteUtils.toHexString(cipherArray);
		return ciperText;
	}
	public static byte [] encrypt_Ecb_Padding(byte[]key,byte[]data)throws Exception{
		Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.ENCRYPT_MODE, key);
		return cipher.doFinal(data);
	}
	public static String decryptEcb(String hexKey,String cipherText) throws Exception{
		String decryptStr = "";
		byte [] keyData = ByteUtils.fromHexString(hexKey);
		byte [] cipherData = ByteUtils.fromHexString(cipherText);
		byte [] srcData = decrypt_Ecb_Padding(keyData, cipherData);
		decryptStr = new String(srcData, ENCODING);
		return decryptStr;
	}
	public static byte [] decrypt_Ecb_Padding(byte[]key,byte[]cipherText) throws Exception{
		Cipher cipher = generateEcbCipher(ALGORITHM_NAME_ECB_PADDING, Cipher.DECRYPT_MODE, key);
		return cipher.doFinal(cipherText);
	}
	
	public static boolean verifyEcb(String hexKey,String cipherText,String paramStr) throws Exception{
		boolean flag = false;
		byte [] keyData = ByteUtils.fromHexString(hexKey);
		byte [] cipherData = ByteUtils.fromHexString(cipherText);
		byte [] decryptData = decrypt_Ecb_Padding(keyData, cipherData);
		byte [] srcData = paramStr.getBytes(ENCODING);
		flag = Arrays.equals(decryptData, srcData);
		
		return flag;
	}
	
	public static void main(String[] args) throws Exception{
		String str = "000120181222004000";
		String key = "56245626B3A9E3F8A97892F3A8B33265";
		String cipher = Sm4Util.encryptEcb(key, str);
		System.err.println(cipher);
		str = Sm4Util.decryptEcb(key, cipher);
		System.err.println(str);
	}
	
	
	
	
	
}
